import { _decorator, Component, Node, Collider, ITriggerEvent } from 'cc'
import { Bullet } from '../bullet/Bullet'
import { Constant } from '../framework/Constant'
import { GameManager } from '../framework/GameManager'
const { ccclass, property } = _decorator

@ccclass('boss')
export class boss extends Component {
  speedX: number = 0.1 // X轴移动速度
  speedZ: number = 0 // Z轴移动速度

  maxScopedL: number = -20 // 最大左边距
  maxScopedR: number = 20 // 最大右边距
  direction: string = 'right' // x轴移动方向

  emitSpace: number = 0.8 // 子弹发射间隔
  timeRecord: number = 0 // 子弹时间记录

  initBloodVolume: number = 500 // 初始血量
  bloodVolume: number = this.initBloodVolume // 当前血量

  upgrade: number = null // 难度升级血量

  difficulty: number = 1 // 难度

  isStart: boolean = false // 是否开始
  death: boolean = false // boss是否死亡

  gameManager: GameManager = null

  @property(Node)
  face: Node = null
  @property(Node)
  explodeNode: Node = null

  start() {
    this.upgrade = this.initBloodVolume / 3
    this.bossEnter() // boss 进场
  }

  update(deltaTime: number) {
    if (this.isStart) {
      if (this.bloodVolume > 0) {
        this.bossMove(deltaTime)
        this.emitBullet(deltaTime)
      }
    } else {
      this.bossEnter()
    }
  }

  // 发射子弹
  emitBullet(deltaTime: number) {
    this.timeRecord += deltaTime
    if (this.timeRecord > this.emitSpace) {
      this.timeRecord = 0
      this.gameManager.bossEmitBullet(this.node, this.difficulty)
    }
  }

  onEnable() {
    // 获取碰撞组件
    const collider = this.getComponent(Collider)
    // 监听开始碰撞
    collider.on('onTriggerEnter', this.onTriggerEnter, this)
  }

  onDisable() {
    const collider = this.getComponent(Collider)
    collider.off('onTriggerEnter', this.onTriggerEnter, this)
  }

  // 碰撞处理
  onTriggerEnter(event: ITriggerEvent) {
    if (!this.isStart) return
    const group = event.otherCollider.getGroup()
    if (group === Constant.collisionGroup.MY_BULLET) {
      const bulletCom = event.otherCollider.getComponent(Bullet)
      this.bloodVolume -= bulletCom.hurt

      // 设置难度
      if (this.bloodVolume < this.upgrade * 2 && this.bloodVolume > this.upgrade) {
        this.difficulty = 2
        this.speedX = 0.2
      } else if (this.bloodVolume < this.upgrade) {
        this.difficulty = 3
        this.speedX = 0.3
      }
      // 设置血条长度
      this.face.setScale(this.bloodVolume * (12 / this.initBloodVolume), 1, 1)
      if (this.bloodVolume > 0) return
      if (!this.death) {
        this.death = true
        // 销毁boss
        this.explodeNode.active = true // 播放死亡效果
        this.schedule(() => {
          this.gameManager.addScore(500) // 增加积分
          this.node.destroy() // 销毁节点
        }, 2)
      }
    }
  }

  // boss移动
  bossMove(deltaTime: number) {
    const pos = this.node.position
    const xMove = this.direction === 'right' ? pos.x + this.speedX : pos.x - this.speedX
    if (xMove > this.maxScopedR) {
      this.direction = 'left'
    }
    if (xMove < this.maxScopedL) {
      this.direction = 'right'
    }
    const zMove = pos.z + this.speedZ
    this.node.setPosition(xMove, pos.y, zMove)
  }
  // boss进场
  bossEnter() {
    let pos = this.node.position
    let moveZ = pos.z + 0.3
    this.node.setPosition(pos.x, pos.y, moveZ)
    if (moveZ >= -30) {
      this.isStart = true
    }
  }
  set(gameManager: GameManager) {
    this.gameManager = gameManager
  }
}
